// components/search/index/index.js
import api from "../../../api/index";
const { debounce } = require("../../../utils/util");
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    nums: {
      type: Object,
      value: () => {
        return {};
      },
    },
    search: {
      type: Array,
      value: () => {
        return [];
      },
    },
    searchExplain: {
      type: null,
      value: null,
    },
    numsExplain: {
      type: null,
      value: null,
    },
    param: {
      type: Object,
      value: () => {
        return {};
      },
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    searchParam: {},
    defaultParam: {},
    dateShow: false,
    topDate: null,
    topShop: null,
    sList: [],
    nList: [],
    haveShow: false,
    numShow: false,
    explainShow: false,
    explainData: null,
    topDateObj: {},
  },
  observers: {
    async search(v) {
      let arr = [[]],
        topDate = null,
        topShop = null,
        span = 0;
      for (let i of v) {
        if (i.type === "topDate") {
          topDate = i;
        } else if (i.type === "topShop") {
          topShop = i;
        } else {
          i.span = i.span || 12;
          if (span + i.span > 24) {
            arr.push([]);
            span = i.span;
          } else {
            span += i.span;
          }
          arr[arr.length - 1].push(i);
        }
      }
      if (arr[0].length) {
        if (span + 8 > 24) {
          arr.push([{ type: "reset", span: 8 }]);
        } else {
          arr[arr.length - 1].push({
            type: "reset",
            span: 8,
            offset: 16 - span,
          });
        }
      }

      this.setData({
        sList: arr,
        topDate,
        topShop,
      });
      if (topDate) topDate.list = await this.getItemList("apiAssistTimeForm");
      if (topShop) {
        let res = await this.getItemList("apiStoreShopList");
        topShop.list = res.list;
      }
      let coachList = null;

      for (let a of arr) {
        for (let i of a) {
          if (i.name === "date") {
            i.list =
              topDate?.list || (await this.getItemList("apiAssistTimeForm"));
          }
          if (
            ["assign_coach_id", "guide_coach_id", "coach_id"].indexOf(
              i.name
            ) !== -1
          ) {
            if (!coachList) {
              let res = await this.getItemList("apiStoreCoachList");
              coachList = res.list;
            }
            i.list = coachList;
          }
          if (i.name === "vip_form")
            i.list = await this.getItemList("apiAssistVipForm");

          if (i.name === "shop_id") {
            if (topShop?.list) {
              i.list = topShop.list;
            } else {
              let res = await this.getItemList("apiStoreShopList");
              i.list = res.list;
            }
          }
          if (i.name === "redeem_status")
            i.list = await this.getItemList("apiAssistRedeemStatus");
          if (i.name === "finance")
            i.list = await this.getItemList("apiAssistFinanceSecond");
          if (i.name === "assign_form")
            i.list = await this.getItemList("apiAssistAssignForm");
          if (i.name === "guide_form")
            i.list = await this.getItemList("apiAssistGuideForm");
          if (i.name === "vip_course")
            i.list = await this.getItemList("apiAssistVipCourse");
          if (i.name === "vip_status")
            i.list = await this.getItemList("apiAssistVipStatus");
          if (i.name === "course_status")
            i.list = await this.getItemList("apiAssistCourseStatus");
          if (i.name === "class_action")
            i.list = await this.getItemList("apiAssistClassAction");
          if (i.name === "pay_status")
            i.list = await this.getItemList("apiAssistPayStatus");
          if (["source", "vip_source"].indexOf(i.name) !== -1)
            i.list =
              i.branch === "redeem"
                ? await this.getItemList("apiAssistRedeemSource")
                : await this.getItemList("apiAssistSource");
          if (i.name === "card_form")
            i.list = await this.getItemList("apiAssistCardForm");
          if (i.name === "card_status")
            i.list = await this.getItemList("apiAssistCardStatus");
          if (i.name === "duration_form")
            i.list = await this.getItemList("apiAssistDurationForm");
          if (i.name === "is_to")
            i.list = await this.getItemList("apiAssistSyncForm");
          if (i.name === "audit")
            i.list = await this.getItemList("apiAssistAuditStatus");
          if (i.name === "reach_form")
            i.list = await this.getItemList("apiAssistReachForm");
          if (i.name === "sign_in")
            i.list = await this.getItemList("apiAssistSignInStatus");
          if (i.name === "on_site")
            i.list = await this.getItemList("apiAssistIsArrive");
          if (i.name === "carry_duration_form")
            i.list = await this.getItemList("apiAssistCarryDurationForm");
        }
      }
      this.setData({
        sList: arr,
        topDate,
        topShop,
      });
    },
    nums(v) {
      let arr = [[]],
        haveShow = false;
      v.forEach((i) => {
        if (i.show) haveShow = true;
        i.span = 8;
        if (arr[arr.length - 1].length >= 3) arr.push([]);
        arr[arr.length - 1].push(i);
      });
      if (arr[arr.length - 1].length !== 3) {
        arr[arr.length - 1] = arr[arr.length - 1].map((i) => {
          i.span = arr[arr.length - 1].length === 1 ? 24 : 12;
          return i;
        });
      }
      this.setData({
        nList: arr,
        haveShow,
      });
    },
    param(v) {
      this.setData({
        searchParam: JSON.parse(JSON.stringify(v)),
      });
    },
    searchParam: debounce(function (v) {
      if (JSON.stringify(v) !== JSON.stringify(this.data.param)) {
        this.triggerEvent("search", v);
      }
    }, 500),
  },
  attached() {
    this.setData({
      defaultParam: JSON.parse(JSON.stringify(this.data.param)),
    });
  },
  /**
   * 组件的方法列表
   */
  methods: {
    getValue(e) {
      let param = this.data.searchParam;
      let { type, value } = e.detail;
      param[type] = value;
      this.setData({
        searchParam: param,
      });
      let item = this.data.search.find((i) => i.name === type);
      if (item?.offRefresh) {
        let key = `defaultParam.${type}`;
        this.setData({
          [key]: value,
        });
      }
    },

    getItemList(e, param = {}) {
      return new Promise((resolve, reject) => {
        api[e](param).then((res) => {
          resolve(res.data);
        });
      });
    },

    openDate() {
      this.setData({
        dateShow: true,
      });
    },
    resetSearch() {
      this.setData({
        searchParam: JSON.parse(JSON.stringify(this.data.defaultParam)),
      });
    },
    dateShowClose() {
      this.setData({
        dateShow: false,
      });
    },
    switchNum(e) {
      this.setData({
        numShow: e.currentTarget.dataset.type == 0,
      });
    },
    explainOpen(e) {
      let name = e.currentTarget.dataset.name;
      this.setData({
        explainData: this.data[name],
        explainShow: true,
      });
    },
    explainClose() {
      this.setData({
        explainShow: false,
      });
    },
  },
});
